package com.liao.starapp.views;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.liao.starapp.R;

public class CircleImageView1 extends androidx.appcompat.widget.AppCompatImageView {

    private int r; //半径

    private int borderW = 0;
    private int borderColor;
    private boolean isDrawBorder;

    public CircleImageView1(Context context) {
        super(context);
        initData();
    }

    public CircleImageView1(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleImageView1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView1, defStyleAttr, 0);
        borderColor = typedArray.getColor(R.styleable.CircleImageView1_borderColor, Color.BLACK);
        borderW = typedArray.getInteger(R.styleable.CircleImageView1_borderWidth, 2);
        isDrawBorder = typedArray.getBoolean(R.styleable.CircleImageView1_drawBorder, true);
        typedArray.recycle();
        initData();
    }

    public void setDrawBorder(boolean isDraw) {
        isDrawBorder = isDraw;
    }

    public void setBorderColor(int color){
        this.borderColor = color;
    }

    public void setBorderW(int borderW){
        this.borderW = borderW;
    }

    private void initData() {

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //由于是圆形，宽高应保持一致
        int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
        r = size / 2;
        setMeasuredDimension(size + borderW, size + borderW);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        Paint mPaint = new Paint();

        Drawable drawable = getDrawable();

        if (null != drawable) {
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();

            //初始化BitmapShader，传入bitmap对象
            BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            //计算缩放比例
            float mScale = (r * 2.0f) / (Math.min(bitmap.getHeight(), bitmap.getWidth()) - borderW / 2.0f);

            Matrix matrix = new Matrix();
            matrix.setScale(mScale, mScale);
            bitmapShader.setLocalMatrix(matrix);
            mPaint.setShader(bitmapShader);
            //画圆形，指定好坐标，半径，画笔
            canvas.drawCircle(r + borderW / 2.0f, r + borderW / 2.0f, r - borderW / 2.0f, mPaint);
            if (isDrawBorder) {
                drawCircleBorder(canvas);
            }
        } else {
            super.onDraw(canvas);
        }
    }

    public void drawCircleBorder(Canvas canvas) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(borderW);
        paint.setColor(borderColor);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(getWidth() / 2.0f, getHeight() / 2.0f, r, paint);
    }

}
